package com.example.spirngbootbase.service;

import com.example.spirngbootbase.bean.LoginUser;
import com.example.spirngbootbase.bean.SysUser;
import com.example.spirngbootbase.config.redis.RedisCache;
import com.example.spirngbootbase.handle.ResponseResult;
import com.example.spirngbootbase.mapper.SysUserMapper;
import com.example.spirngbootbase.util.JwtUtil;
import lombok.val;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Objects;

/**
 * @ClassName LoginService
 * @Description TODO
 * @Author mujiechen
 * @Date 2022/7/28
 */
@Service
public class LoginService {
    @Resource
    SysUserMapper sysUserMapper;

    @Resource
    AuthenticationManager authenticationManager;

    @Resource
    RedisCache redisCache;

    /**
     * 登录
     * @param sysUser
     * @return
     */

    public ResponseResult login(SysUser sysUser){
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken=
                new UsernamePasswordAuthenticationToken(sysUser.getUserName(),sysUser.getPassword());
        Authentication authentication=authenticationManager.authenticate(usernamePasswordAuthenticationToken);
        if(Objects.isNull(authentication)){
            throw new RuntimeException("账号或密码错误");
        }
        //使用userId生成token
        LoginUser loginUser=(LoginUser)authentication.getPrincipal();
        String userId=loginUser.getSysUser().getId().toString();
        String jwt=JwtUtil.createJWT(userId);
        //把用户信息放在redis中
        redisCache.setCacheObject("login"+userId,loginUser);
        //把token响应给前端
        HashMap<String,String> map = new HashMap<>();
        map.put("token",jwt);
        return new ResponseResult(200,"登陆成功",map);
    }

    /**
     * 退出登录
     * @return
     */
    public ResponseResult logOut(){
        //通过token退出登录 每个接口都会携带token，在过滤中用户信息已经存在SecurityContextHolder
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
         LoginUser loginUser= (LoginUser) usernamePasswordAuthenticationToken.getPrincipal();
       redisCache.deleteObject("login"+loginUser.getSysUser().getId());
       return new ResponseResult(200,"退出成功");
    }

//    public ResponseResult addUser(SysUser sysUser){
//        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder(sysUser.getPassword());
//        sysUser.setPassword();
//    }

}
